// pages/index/index.js
import Butils from '../../utils/detailsUtil'
import utils from '../../utils/util'
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoad: false,
    list: [],
    date: "",
    startDate: null,
    endDate: null,
    maxDate: "",
    amount: 0,
    inAmount: 0,
    outAmount: 0.00,
    dateArray: [
      [],
      []
    ],
    dateIndex: [0, 0]
  },
  getList: function (year, month) {
    let that = this;
    let innum = 0;
    let outnum = 0;
    Butils.getDetails(app.globalData.wxUserInfo.id, null, year, month, (res) => {
      res.data.objects.forEach(e => {
        if (e.bill_type.is_income) {
          innum += Number(e.amount)
        } else {
          outnum += Number(e.amount)
        }
      });
      that.setData({
        list: res.data.objects,
        amount: parseFloat(innum - outnum).toFixed(2),
        inAmount: parseFloat(innum).toFixed(2),
        outAmount: parseFloat(outnum).toFixed(2)
      })
    })
  },
  DateChange: function (e) {
    this.setData({
      date: e.detail.value,
      startDate: e.detail.value,
      endDate: utils.formatDateMAdd1(e.detail.value)
    })
  },
  DateMultiChange(e) {
    this.setData({
      dateIndex: e.detail.value
    })
    this.getList(this.data.dateArray[0][e.detail.value[0]], this.data.dateArray[1][e.detail.value[1]]);
  },
  /**
   * 查看账单明细
   * @param {*} params 
   */
  BillInfo: function (e) {
    if (e.target.dataset.target) {
      app.globalData.billInfo = e.target.dataset.target
      wx.navigateTo({
        url: '../bill/billInfo/billInfo'
      })
    }
  },
  /**
   * 新增账单
   * @param {*} e 
   */
  AddBill: function (e) {
    app.globalData.billInfo = null
    app.globalData.isAddBill = true
    wx.navigateTo({
      url: '../add/add'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let arr = [[], []]
    let nowYear = (new Date()).getFullYear();
    let nowMonth = (new Date()).getMonth();
    for (let i = 10; i >= 0; i--) {
      arr[0].push(nowYear - i)
    }
    for (let i = 1; i <= 12; i++) {
      arr[1].push(i)
    }
    this.setData({
      date: utils.formatDateM(new Date()),
      maxDate: utils.formatDateM(new Date()),
      startDate: utils.formatDateM(new Date()),
      endDate: utils.formatDateMAdd1(new Date()),
      dateArray: arr,
      dateIndex: [10, nowMonth],
      isLoad: true
    })

    if (app.globalData.wxUserInfo) {
      this.getList(this.data.dateArray[0][this.data.dateIndex[0]], this.data.dateArray[1][this.data.dateIndex[1]]);
    }
    // 给app.js 定义一个方法。
    app.userInfoReadyCallback = res => {
      if (app.globalData.wxUserInfo) {
        this.getList(this.data.dateArray[0][this.data.dateIndex[0]], this.data.dateArray[1][this.data.dateIndex[1]]);
      }
    };
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.wxUserInfo) {
      this.getList(this.data.dateArray[0][this.data.dateIndex[0]], this.data.dateArray[1][this.data.dateIndex[1]]);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})